/**
 * $RCSfile:  $
 * $Revision: $
 * $Date:  $
 *
 * Copyright (C) 2006 Jay Kline. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.wildfire.plugin;

import org.jivesoftware.wildfire.container.Plugin;
import org.jivesoftware.wildfire.container.PluginManager;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.PropertyEventDispatcher;
import org.jivesoftware.util.PropertyEventListener;
import org.jivesoftware.wildfire.SessionManager;
import org.jivesoftware.wildfire.event.SessionEventDispatcher;
import org.jivesoftware.wildfire.event.SessionEventListener;
import org.jivesoftware.wildfire.Session;

import org.jivesoftware.wildfire.XMPPServer;

import org.xmpp.packet.*;

import java.io.File;

import java.util.*;


/**
 * Message of the day plugin. It sends a message to all users when they log in. 
 *
 * @author Jay Kline
 */
public class MotdPlugin implements SessionEventListener, PropertyEventListener, Plugin {
    
    private String serviceName;
    private SessionManager sessionManager;
    private List<JID> skipUsers;
    private String subject;
    private String message;
    private PluginManager pluginManager;
    
    public MotdPlugin() {
        skipUsers = stringToList(JiveGlobals.getProperty("plugin.motd.skipUsers", ""));
        subject   = JiveGlobals.getProperty("plugin.motd.subject");
        message   = JiveGlobals.getProperty("plugin.motd.message");
    }
    
    // Plugin Interface
    
    public void initializePlugin(PluginManager manager, File pluginDirectory) {
        pluginManager = manager;
        sessionManager = SessionManager.getInstance();

        PropertyEventDispatcher.addListener(this);
        SessionEventDispatcher.addListener(this);
    }
        
    public void destroyPlugin() {
        PropertyEventDispatcher.removeListener(this);
        SessionEventDispatcher.removeListener(this);
        pluginManager = null;
        sessionManager = null;
        skipUsers.clear();
    }
       
    // SessionEventListener interface
    
    public void anonymousSessionCreated(Session session) {
    }
    
    public void anonymousSessionDestroyed(Session session) {
    }
    
    public void sessionCreated(Session session) {
        JID jid = session.getAddress();
        
        if(message == null || skipUsers.contains(jid)) {
            // nothing to do
            return;
        }
        
        Message motd = new Message();
        motd.setType(Message.Type.normal);
        motd.setFrom(JiveGlobals.getProperty("xmpp.domain"));
        motd.setTo(jid);
        motd.setSubject(subject);
        motd.setBody(message);
        XMPPServer.getInstance().getMessageRouter().route(motd);
        
    }
    
    public void sessionDestroyed(Session session) {
    }
    
    // PropertyEventListener interface

    public void propertySet(String property, Map params) {
        if (property.equals("plugin.motd.skipUsers")) {
            this.skipUsers = stringToList((String)params.get("value"));
        }
        else if (property.equals("plugin.motd.subject")) {
            this.subject = (String)params.get("value");
        }
        else if (property.equals("plugin.motd.message")) {
            this.message = (String)params.get("value");
        }
    }

    public void propertyDeleted(String property, Map params) {
        if (property.equals("plugin.motd.skipUsers")) {
            this.skipUsers = Collections.emptyList();
        }
        else if (property.equals("plugin.motd.subject")) {
            this.subject = null;
        }
        else if (property.equals("plugin.motd.message")) {
            this.message = null;
        }
    }

    public void xmlPropertySet(String property, Map params) {
        // Ignore.
    }

    public void xmlPropertyDeleted(String property, Map params) {
        // Ignore.
    }
    
    // Other methods
    
    private List<JID> stringToList(String str) {
        List<JID> values = new ArrayList<JID>();
        StringTokenizer tokens = new StringTokenizer(str, ",");
        while (tokens.hasMoreTokens()) {
            String value = tokens.nextToken().trim();
            if (!value.equals("")) {
                // See if this is a full JID or just a username.
                if (value.contains("@")) {
                    values.add(new JID(value));
                }
                else {
                    values.add(XMPPServer.getInstance().createJID(value, null));
                }
            }
        }
        return values;
    }
}
